<?php

abstract class Base_Base implements Base_Custom
{
    private $_singleton = array();
    public function _classBase()
    {
        return preg_replace('#(.*)_((.*?)){1}$#', '$2', get_class($this));
    }
    public function _customName()
    {
        return preg_replace('#(.*)_((.*?)){1}$#', '$1', get_class($this));
    }
    protected function _hasVisibleProperty($propertyName)
    {
        $property = new ReflectionProperty(get_class($this), $propertyName);
        if ($property->isPrivate() || $property->isProtected()) return FALSE;
        return TRUE;
    }
    protected function _hasVisibleMethod($methodName)
    {
        $method = new ReflectionMethod(get_class($this), $methodName);
        if ($method->isPrivate() || $method->isProtected()) return FALSE;
        return TRUE;
    }
    public function __get($i) 
    {
        if (class_exists('Base_'.$i)) {
            //Classname is the custom name of the class plus the request
            $className = $this->_customName().'_'.ucfirst($i);
            //No singletons already defined, lets create one
            if (!isset($this->_singleton[$i])) $this->_singleton[$i] = new $className;
            //Return that singleton
            return $this->_singleton[$i];
        }
    }
    public function __call($i,$a) 
    {
        if (class_exists('Base_'.$i)) {
            //No singletons already defined, lets create one
            if (!isset($this->_singleton[$i])) {
                //Reflecting the class
                $classMirror = new ReflectionClass($this->_customName().'_'.$i);
                //Creates new instance
                $this->_singleton[$i] = $classMirror->newInstanceArgs($a);
            }
            //Return that singleton
            return $this->_singleton[$i];
        }
    }
}

?>
    